﻿using System;

using System.Management.Automation;

using CrystalDecisions.Enterprise;

namespace BO.Management.PowerShell
{
    [Cmdlet(VerbsCommon.Get, "Instance", DefaultParameterSetName = CONNECT_SECWINAD)]
    public class GetInstance : BaseGetObject<InstanceProvider, CrystalDecisions.Enterprise.InfoObject>
    {

        [Parameter()]
        public SwitchParameter Recuring { get; set; }

        [Parameter()]
        public DateTime? Start { get; set; }

        [Parameter()]
        public DateTime? End { get; set; }

        [Parameter()]
        public SwitchParameter Success { get; set; }

        [Parameter()]
        public SwitchParameter Failure { get; set; }

        [Parameter()]
        public SwitchParameter Pending { get; set; }


        [Parameter()]
        public SwitchParameter ViewSchedule { get; set; }


        public override InstanceProvider GetProvider()
        {
            if (this.Recuring)
            {
                return new RecuringInstanceProvider(this.LogonProvider);
            }
            else
            {
                return new InstanceProvider(this.LogonProvider);
            }
        }

        protected override void FindBoeObject()
        {
            //Load additional parameters
            if (Start.HasValue)
                BoeProvider.AddStartTime(Start.Value);

            if (End.HasValue)
                BoeProvider.AddEndTime(End.Value);
            else
            {
                if (Start.HasValue & Failure)
                {
                    BoeProvider.AddEndTime(DateTime.Now);
                }
            }

            if (Success)
                BoeProvider.AddOutComeStatusSuccess();

            if (Failure)
                BoeProvider.AddOutComeStatusFailure();

            if (Pending)
                BoeProvider.AddOutComeStatusPending();

            base.FindBoeObject();
        }

        public override void WorkWithSingleObject(InfoObject boeObject)
        {
            if (!ViewSchedule)
                base.WorkWithSingleObject(boeObject);
            else
            {
                BOScheduleInformation info = new BOScheduleInformation(boeObject.ID, boeObject.Title, boeObject);
                WriteObject(info);
            }
        }

    }
}
